package com.heima.leetcode.practice;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 110. 平衡二叉树
 * @date 2025/5/22 16:25
 */
public class E110 {
    public boolean isBalanced(TreeNode root) {
        return checkHeight(root) != -1;
    }

    private int checkHeight(TreeNode root) {
        if (root == null) return 0;
        int lHeight = checkHeight(root.left);
        int rHeight = checkHeight(root.right);
        if (lHeight == -1 || rHeight == -1) {
            return -1;
        }
        if (Math.abs(lHeight - rHeight) > 1) {
            return -1;
        }
        return Math.max(lHeight, rHeight) + 1;
    }
}
